<?php
defined('SYSPATH') OR die('No direct access allowed.');
/**
 * proseslowongan.php
 * @author ranuandra
 *
 * 6 Mei 2012 12:13:37
 *
 * 
 */

class Controller_Admin_Proseslowongan extends Kohana_Admin
{
	public function action_index()
	{
		$datas = ORM::factory('mstlowongan')->where('mstproses_id', 'IS NOT', NULL)->find_all();
		
		$view = View::factory('admin/proseslowongan/index');
		$view->datas = $datas;

		$this->template->container = $view;
	}
	
	public function action_proses()
	{
		$id = Request::current()->param('id');
		if($id != "")
		{
			$datas = ORM::factory('proseslowongan')->where('mstlowongan_id', '=', $id)->order_by('tanggal', 'ASC')->find_all();
			
			//$proses = $datas->prosespelamar->where('flaglolos', '=', 0)->find_all();
			//print_r($proses);
			$view = View::factory('admin/proseslowongan/lowongan');
			$view->id = $id;
			$view->lowongan = ORM::factory('mstlowongan', $id);
			$view->datas = $datas;
	
			$this->template->container = $view;
		}
	}
	
	public function action_pelamar()
	{
		$id = Request::current()->param('id');
		if($id != "")
		{
			$proses = ORM::factory('proseslowongan', $id);
			$pelamars = $proses->pelamarproses->find_all();
			$view = View::factory('admin/proseslowongan/pelamar');
			$view->id =$id;
			$view->proses = $proses;
			$view->datas = $pelamars;
			$this->template->container = $view;
		}
		
	}
	
	public function action_confirm()
	{
		
		$datas = array();
		$proseses = array();
		//$this->auto_render = false;
		$panggils = Request::current()->post('panggil');
		$hadirs = Request::current()->post('hadir');
		$loloses = Request::current()->post('lolos');
		
		$id = Request::current()->post('proseslowongan_id');
		$lowongan = ORM::factory('proseslowongan', $id);
		
		//ambil data pelamar dan data pelamarproses untuk di tandai
		foreach($panggils as $panggil)
		{
			$dataproses = ORM::factory('pelamarproses', $panggil);
			$datas[] = ORM::factory('pelamarlowongan', $dataproses->pelamarlowongan_id);
			$proseses[] = $dataproses;
		}
		
		//ambil proses yg sudah di jalankan
		$ckdata = ORM::factory('pelamarproses', $panggils[0]);
		$cekproseses = ORM::factory('pelamarproses')->where('pelamarlowongan_id', '=', $ckdata->pelamarlowongan_id)->find_all();
		$prosesed = array();
		foreach($cekproseses as $cek)
		{
			$prosesed[] = $cek->proseslowongan->mstproses_id;
		}	

		
		//ambil proses yg belum dijalankan
		$proseslist = ORM::factory('mstproses');
		foreach($prosesed as $item)
		{
			$proseslist = $proseslist->where('id', '<>', $item);
		}
		
		$proseslist = $proseslist->order_by('urutan', 'ASC')->find_all()->as_array('id', 'proses');
		
		$view = View::factory('admin/proseslowongan/confirm');
		$view->lowongan = $lowongan;
		$view->cabangs = ORM::factory('mstplacement')->find_all()->as_array('id', 'placement');
		$view->users = ORM::factory('user')->where('mstplacement_id', '!=', NULL)->find_all()->as_array('id', 'username');
		$view->id = $id;
		$view->proseslist = $proseslist;
		
		$view->hadirs = $hadirs;
		$view->loloses = $loloses;
		$view->pelamarlowongan_id = $panggils;
		$view->datas = $datas;
		$view->panggils = $panggils;
		
		$this->template->container = $view;
	}

	
	public function action_save()
	{
		if($_POST)
		{
			$db = Database::instance();
			$db->begin();
			try 
			{
				$year = date("Y");
				$month = date("m");
				$mx = DB::select(array('MAX("id")', 'maks'))->from('proseslowongans')->execute();

				$no = $mx[0]['maks'] + 1;
				
				
				
				$proses = ORM::factory('proseslowongan');
				$proses->kode = "PRS-".$year.$month."-".$no;
				$proses->tanggal = Request::current()->post('tgl')." ".Request::current()->post('jam');
				$proses->create_data($_POST);
				$prosesid = $proses->id;
				
				
				
				$pelamars = Request::current()->post('pelamarlowongan_id');
				$hadirs = Request::current()->post('flag_hadir');
				$loloses = Request::current()->post('flaglolos');
				$panggils = Request::current()->post('panggils');
				
				//simpan pelamar untuk proses selanjutnya
				foreach($pelamars as $pelamar)
				{
					
					$pelamarproses = ORM::factory('pelamarproses');
					$pelamarproses->pelamarlowongan_id = $pelamar;
					$pelamarproses->proseslowongan_id = $prosesid;
/*					$pelamarproses->flag_hadir = 1;
					$pelamarptoses->flaglolos = 1;
					$pelamarproses->flag_hadir = 1;*/
					$pelamarproses->save();
					
					$datapelamar = ORM::factory('pelamarlowongan', $pelamar);
					$userid = $datapelamar->mstpelamar->user_id;

/*					$mstpelamar = ORM::factory('mstpelamar', $userid);
					$mstpelamar->status = 2;
					$mstpelamar->save();*/
					
					//echo "test";
					
					//tandai yg lolos ke proses selanjutnya
/*					$ceklolos = ORM::factory('pelamarproses', $pelamar);
					$ceklolos->flag_panggil = 1;
					$ceklolos->update();*/
				}
				
				if(isset($hadirs))
				{
					foreach($hadirs as $hadir)
					{
						$cekhadir = ORM::factory('pelamarproses', $hadir);
						$cekhadir->flag_hadir = 1;
						$cekhadir->update();
					}
				}
				
				if(isset($loloses))
				{
					foreach($loloses as $lolos)
					{
						$ceklolos = ORM::factory('pelamarproses', $lolos);
						$ceklolos->flaglolos = 1;
						$ceklolos->update();
					}
				}
				
				if(isset($panggils))
				{
					foreach($panggils as $panggil)
					{
						$cekpanggil = ORM::factory('pelamarproses', $panggil);
						$cekpanggil->flag_panggil = 1;
						$cekpanggil->update();
					}
				}
				
				if(isset($_POST['mstlowongan_id']))
				{
					$lowongan = ORM::factory('mstlowongan', Request::current()->post('mstlowongan_id'));
					$lowongan->mstproses_id = Request::current()->post('mstproses_id');
					$lowongan->update();
				}
				
				$db->commit();
				//kirim email kepada yg dipanggil
				$proses = ORM::factory('mstproses',Request::current()->post('mstproses_id') );
				foreach($pelamars as $pelamar)
				{	
					$datapelamar = ORM::factory('pelamarlowongan', $pelamar);
					$userid = $datapelamar->mstpelamar->user_id;
					$user = ORM::factory('user', $userid);

					$subject = "Panggilan ". $proses->proses;
					$message = "Anda diminta untuk mengikuti proses ".$proses->proses." dengan data:\n";
					$message .= "Tanggal : ".Request::current()->post('tgl')."\n";
					$message .= "Jam : ".Request::current()->post('jam')."\n";
					$message .= "Lokasi : ".Request::current()->post('alamat')."\n";
					$from = "hrd@nasmoco.com";
					$headers = "From:" . $from;
					//mail($user->email,$subject,$message,$headers);
					
				}					
				
			}
			catch(ORM_Validation_Exception $e)
			{
				$db->rollback();
				$errors = $e->errors('models');
				print_r($errors);				
			}
		}
	}
	
	public function action_xls()
	{
		
		$id = Request::current()->param('id');
		if($id != "")
		{
		    $items = array();
		    $dataarray = array();
		    $alldata = array();
			$proses = ORM::factory('proseslowongan', $id);
			$pelamars = $proses->pelamarproses->find_all();
			
			$no = 1;
			$row = 4;
			
			$dataarray[1] = array('Proses :', $proses->mstproses->proses, 'Tanggal :', $proses->tanggal, '');
			$dataarray[2] = array('PIC :', $proses->pic, 'Alamat :', $proses->alamat, '');
			$dataarray[3] = array('No','Kode', 'Nama', 'Jenis Kelamin', 'Pendidikan');
			
			foreach($pelamars as $pelamar)
			{
				$items = array($no, $pelamar->pelamarlowongan->kode_lamaran, $pelamar->pelamarlowongan->mstpelamar->nama_lengkap, $pelamar->pelamarlowongan->mstpelamar->jenis_kelamin, $pelamar->pelamarlowongan->mstpelamar->mstpendidikan->pendidikan);
				$dataarray[$row] = $items;
				$row++;
				$no++;
			}
			$alldata['Data'] = $dataarray;

			$xlsx = new Spreadsheet();
			$xlsx->set_data($alldata, 1);
			$file = $xlsx->save(array('name'=>'test'));
			
			Response::factory()->send_file($file);
			
		}
		
	}
	
	public function action_pdf()
	{
		$id = Request::current()->param('id');
		if($id != "")
		{

			$proses = ORM::factory('proseslowongan', $id);
			$pelamars = $proses->pelamarproses->find_all();
			
			$view = View::factory('admin/proseslowongan/pdf');
			$view->proses = $proses;
			$view->datas = $pelamars;
			
			
			$fPdf = new Pdf();
			$fPdf->addContent($view);
			$fPdf->setName('test');
			$fPdf->setHeaders();
			$fPdf->render();


			
			exit;
			
		}		
	}
}